
$(function(){

	pagination.setNavUrl('./?controller=user&task=ajax_listing');
	pagination.setDataUrl('./?controller=user&task=ajax_listing_data');
	
	pagination.setLimit(20);	// 每页显示的记录条数
	
	pagination.setUpdateNavFn(function(){

		var s = '';
		
		if(pagination.getTotal()>0)
		{
			s += '<div class="pagination">';
			s += '<span>';
			s += '共'+ pagination.getTotal() + "个用户";
			s += '</span>';
			s += '<span class="first">';
			if(pagination.getPageNo()>1)
				s += '<a href="javascript:pagination.page(1)">&lt;&lt;</a>';
			else
				s += '&lt;&lt;';
			s += '</span>';
			s += '<span class="pre">';
			if(pagination.getPageNo()>1)
				s += '<a href="javascript:pagination.page('+(pagination.getPageNo()-1)+')">&lt;</a>';
			else
				s += '&lt;';
			s += '</span>';
			s += '<span class="current">';
			s += pagination.getPageNo()+"/"+pagination.getMaxPageNo();
			s += '</span>';
			s += '<span class="next">';
			if(pagination.getPageNo()<pagination.getMaxPageNo())
				s += '<a href="javascript:pagination.page('+(pagination.getPageNo()+1)+')">&gt;</a>';
			else
				s += '&gt;';
			s += '</span>';
			s += '<span class="last">';
			if(pagination.getPageNo()<pagination.getMaxPageNo())
				s += '<a href="javascript:pagination.page('+ pagination.getMaxPageNo() +')">&gt;&gt;</a>';
			else
				s += '&gt;&gt;';
			s += '</span>';
			s += '<span class="goto">';
			s += '<select onchange="javascript:pagination.page(this.options[this.selectedIndex].value);">';
			for(var i = 1; i<=pagination.getMaxPageNo(); i++)
			{
				s += '<option value="'+i+'"';
				if(i == pagination.getPageNo())
					s += ' selected';
				s += '>'+i+"</option>";
			}		
			s += '</select>';
			s += '</span>';
			s += '</div>';
		}
		else
			s += '没有记录';
		$("#pagination-nav").html(s);
	});
	
	
	pagination.setUpdateDataFn(function(data){
		var s = '';
		var o;

		var aUserList = data.user_list;
		for(var i=0, n=aUserList.length; i<n; i++)
		{
			o = aUserList[i]
			s += '<tr id="row-'+o.id+'" onMouseOver="this.className=\'row-on\'" onMouseOut="this.className=\'row-off\'">';
			s += '<td>'+o.username+'</td>';
			s += '<td width="80" align="center">'+o.name+'</td>';
			s += '<td width="200" align="center">'+o.email+'</td>';
			s += '<td width="120" align="center">'+o.register_time+'</td>';
			s += '<td width="120" align="center">'+o.last_visit_time+'</td>';
			s += '<td width="60" align="center"><a href="javascript:;" onclick="javascript:setAdmin('+o.id+')" class="status-'+o.is_admin+'" id="status-'+o.id+'"></a></td>';
			s += '<td width="60" align="center"><a href="javascript:;" onclick="javascript:setBlock('+o.id+')" class="block-'+o.block+'" id="block-'+o.id+'"></a></td>';
			s += '<td width="40" align="center"><a href="./?controller=user&task=edit&user_id='+o.id+'" class="edit" title="修改"></a></td>';
			s += '<td width="40" align="center"><a href="javascript:;" onclick="javascript:deleteUser('+o.id+')" class="delete" title="删除"></a></td>';
			s += '</tr>';
		}
		
		$("#user-list").append(s);
	});
	
	$("#pagination-nav").html("加载中...");
	pagination.init();
});


function searchUser()
{
	var sKey = $.trim($("#key").val());
	
	if(sKey!="")
	{
		pagination.setNavUrl('./?controller=user&task=ajax_listing&key='+sKey);
		pagination.setDataUrl('./?controller=user&task=ajax_listing_data&key='+sKey);
	}
	else
	{
		pagination.setNavUrl('./?controller=user&task=ajax_listing');
		pagination.setDataUrl('./?controller=user&task=ajax_listing_data');
	}

	$("#user-list").html("");
	$("#pagination-nav").html("搜索中...");
	pagination.init();
}


function setAdmin(iUserID)
{
	var e=$("#status-"+iUserID);
	var iNewStatus = 0;
	if(e.attr("class")=="status-0") iNewStatus = 1;

	$.ajax({
		type: "GET",
		url: "./?controller=user&task=ajax_set_admin&user_id="+iUserID+"&status="+iNewStatus,
		dataType: "json",
		success: function(json){
			if(json.status=="0")
			{
				e.attr("class", "status-"+iNewStatus);
			}
			else
			{
				alert(json.description);
			}
		}
	});	
}

function setBlock(iUserID)
{
	var e=$("#block-"+iUserID);
	var iBlock = 0;
	if(e.attr("class")=="block-0") iBlock = 1;

	$.ajax({
		type: "GET",
		url: "./?controller=user&task=ajax_set_block&user_id="+iUserID+"&block="+iBlock,
		dataType: "json",
		success: function(json){
			if(json.status=="0")
			{
				e.attr("class", "block-"+iBlock);
			}
			else
			{
				alert(json.description);
			}
		}
	});	
}


// 删除菜单
function deleteUser(iUserID)
{
	if(confirm("确认要删除该用户吗?"))
	{
		$.ajax({
			type: "GET",
			url: "./?controller=user&task=ajax_delete&user_id="+iUserID,
			dataType: 'json',
			success: function(json){
				if(json.status=="0")
				{
					$("#row-"+iUserID).remove();
				}
				else
				{
					alert(json.description);
				}			
			}
		});	
	}
}
















